#!/bin/bash
# Author    : MoShangSan
# create_ts : 2022-05-15
# program   : Compile Code, Convert DOS/Mac text file format into Unix, Tar package
bash_path=$(cd `dirname $0`; pwd)
CURRENT_PATH=$(cd ${bash_path}/..; pwd)
INSTALL_PATH=${CURRENT_PATH}/output
CURRENT_TIME=$(date "+%Y%m%d_%H%M")
SHELL_DIR_PATH="/data/code/Shell"
LOG_FILE=${CURRENT_PATH}/log/$(date "+%Y%m%d%H%M%S").log

SOURCE_DIR=$1
CODECHECK_DIR=$2
IS_PUSH_CHAT=$3

function LOG() {
    USER=$(whoami) ####那个用户在操作
    if [ ! -e ${CURRENT_PATH}/log ];then
       mkdir ${CURRENT_PATH}/log
    fi
    if [ ! -f ${LOG_FILE} ];then
       touch ${LOG_FILE}
    fi
    echo "[${DATE}] ${USER} [$1] $2"
    echo "[${DATE}] ${USER} [$1] $2" >> ${LOG_FILE}  ######（$0脚本本身，$@将参数作为整体传输调用, >> 将输出结果以追加的方式添加重定向到本地文件中）
}

function dos_to_unix() {
    # Convert DOS/Mac text file format into Unix under built-in `dos2unix` handler.
        # $1: OS directory path
    local path="$1"
    local path=${path%/}
    local IFS=$'\n'
    # shellcheck disable=SC2045
    for obj in $(ls "${path}")
    do
        if [[ -d "${path}/${obj}" ]]; then
            dos_to_unix "${path}/${obj}"
        else
            dos2unix "${path}/${obj}"
        fi
    done
}

function codecheck() {
    # 代码检查
    cd ${CURRENT_PATH}
    mkdir -p output
    mkdir -p ${CODECHECK_DIR}/codecheck
    mkdir -p ${CODECHECK_DIR}/codecheck/${CURRENT_TIME}
    pylint -f parseable src/  -d I0011,R0801 --evaluation="error + warning + refactor + convention"| tee output/pylint.xml
    mv output/pylint.xml ${CODECHECK_DIR}/codecheck/${CURRENT_TIME}/pylint.xml
    LOG INFO "Pylint Finish"

    sloccount --duplicates --details ${CODECHECK_DIR}/src > ${CODECHECK_DIR}/codecheck/${CURRENT_TIME}/sloccount_.sc
    LOG INFO "Sloccount Finish"
}

function main() {
    cd $SERVICE_PATH
    mkdir -P $SERVICE_PATH/log
    GIT_UPDATE_LOG=$(git log -1 --format='%s')
    LOG INFO $GIT_UPDATE_LOG
    # service服务安装 
    bash $SHELL_INSTALL_PATH/chat_robot.sh $PROJECT_PATH "[GIT]$GIT_UPDATE_LOG" "[JENKINS] Build success, url: http://buzhoushan.local:8080/job/Silver_Star/"
    if [ -d $INSTALL_PATH ];then
        rm -rf $INSTALL_PATH/*
        LOG INFO "service file remove"
    fi 
    cp -rf $SERVICE_PATH/*  $INSTALL_PATH
    # 代码服务安装
    if [ -d $CODECHECK_DIR ];then
        rm -rf $CODECHECK_DIR/src/*
        LOG INFO "service file remove"
    fi 
    cp -rf $SERVICE_PATH/*  $CODECHECK_DIR
    codecheck

    # shell脚本安装
    cp -rf $SERVICE_PATH/plugins/docker_service/*  $SHELL_INSTALL_PATH
    LOG INFO "shell install success"
    if [ $IS_PUSH_CHAT == "TRUE" ];then
        if [ $RES -eq 0 ];then
            bash $SHELL_INSTALL_PATH/chat_robot.sh "Silver_Star" "[JENKINS]Build_Success" "url:http://buzhoushan.local:8080/job/Silver_Star/"
        else
            bash $SHELL_INSTALL_PATH/chat_robot.sh "Silver_Star" "[JENKINS]Build_Fail,res:$RES" "url:http://buzhoushan.local:8080/job/Silver_Star/"
        fi
    fi
    if [ $IS_PUSH_CHAT == "TRUE" ];then
        bash $SHELL_INSTALL_PATH/chat_robot.sh "Silver_Star" "[JENKINS]Build_Success" "url:http://buzhoushan.local:8080/job/Silver_Star/"
    fi
}

LOG INFO "--Start build.sh --------"
main